package cn.iocoder.yudao.module.wms.service.vehicle;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.wms.controller.admin.vehicle.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.vehicle.VehicleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.vehicle.VehicleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 车辆资料 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, VehicleDO> implements VehicleService {

    @Resource
    private VehicleMapper vehicleMapper;

    @Override
    public String createVehicle(VehicleSaveReqVO reqVO) {
        // 查询车牌号码是否存在
        boolean exists = this.exists(Wrappers.lambdaQuery(VehicleDO.class)
                .eq(VehicleDO::getNumber, reqVO.getNumber().trim())
        );
        if (exists) {
            throw exception(VEHICLE_NUMBER_EXISTS);
        }

        // 插入
        VehicleDO vehicleDO = BeanUtils.toBean(reqVO, VehicleDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        vehicleDO.setId(UUID.randomUUID().toString())
                .setDriving(false)
                .setCreateAt(now)
                .setUpdateAt(now);
        vehicleMapper.insert(vehicleDO);
        // 返回
        return vehicleDO.getId();
    }

    @Override
    public void updateVehicle(VehicleSaveReqVO reqVO) {
        // 校验存在
        validateVehicleExists(reqVO.getId());
        // 查询车牌号码是否存在
        boolean exists = this.exists(Wrappers.lambdaQuery(VehicleDO.class)
                .ne(VehicleDO::getId, reqVO.getId())
                .eq(VehicleDO::getNumber, reqVO.getNumber().trim())
        );
        if (exists) {
            throw exception(VEHICLE_NUMBER_EXISTS);
        }
        // 更新
        VehicleDO updateObj = BeanUtils.toBean(reqVO, VehicleDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        vehicleMapper.updateById(updateObj);
    }

    @Override
    public void deleteVehicle(String id) {
        // 校验存在
        validateVehicleExists(id);
        // 删除
        vehicleMapper.deleteById(id);
    }

    private void validateVehicleExists(String id) {
        if (vehicleMapper.selectById(id) == null) {
            throw exception(VEHICLE_NOT_EXISTS);
        }
    }

    @Override
    public VehicleDO getVehicle(String id) {
        return vehicleMapper.selectById(id);
    }

    @Override
    public PageResult<VehicleDO> getVehiclePage(VehiclePageReqVO pageReqVO) {
        return vehicleMapper.selectPage(pageReqVO);
    }

}